﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using OpenExpressApp.Editors;
using System.Windows.Data;
using Csla.Wpf;
using System.Windows;
using OpenExpressApp.Module.WPF.Converter;

namespace OpenExpressApp.Module.WPF.Editors
{
    public class StringPropertyEditor : WPFPropertyEditor
    {
        private TextBox _tb;

        public StringPropertyEditor(BusinessObjectPropertyInfo propertyInfo, IObjectView view)
            : base(propertyInfo, view) { }

        protected override FrameworkElement CreateEditingElement()
        {
            this._tb = new TextBox()
            {
                Name = PropertyInfo.Name,
            };

            //绑定TextBox到对象属性
            Binding textBinding = new Binding(PropertyInfo.Name);

            //for debug
          //  textBinding.Converter = new DebuggingConverter();
            //如果是只读属性，则设置binding mode
            if (this.PropertyInfo.RuntimeProperty.CanWrite == false)
            {
                //这里如果不会到达的话，就可以删除了。
                textBinding.Mode = BindingMode.OneWay;
            }
            this._tb.SetBinding(TextBox.TextProperty, textBinding);
            //当类库属性PropertyInfo.Name的set赋值过程更改了value时，需要强制将数据从绑定源属性传输到绑定目标属性。
            _tb.LostFocus += delegate(object sender, RoutedEventArgs e)
            {
                _tb.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
            };

            return _tb;
        }

    }
}